﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using BrokerBase.Lib.Query;
using BrokerBase.Lib.Core;
using NHibernate;

namespace Broker.View.Comm
{
    /// <summary>
    /// ListSelForm.xaml 的交互逻辑
    /// </summary>
    public partial class ListSelForm 
    {

        private const int PAGE_SIZE = 19;

        //private object selObj;

        private Action action;

        public ListSelForm()
        {
            InitializeComponent();
            dpList.PageSize = PAGE_SIZE;
            Loaded += new RoutedEventHandler(ListSelForm_Loaded);
        }

        void ListSelForm_Loaded(object sender, RoutedEventArgs e)
        {
            
        }


        public RadGridView GridView 
        {
            get { return this.gvList; } 
        }


        public T SelectByList<T>(IList<T> list)
        {
            T obj = default(T);

            gvList.ItemsSource = list;


            action = () =>
                {
                    obj = (T)gvList.SelectedItem;

                    if (obj != null)
                    {
                        Close();
                    }
                };

            ShowWindow(true);
            return obj;
        }


        public T SelectByHQLWithPaging<T>(PagingQuery pq) where T:class
        {
            T result = null;

            gvList.SelectionMode = SelectionMode.Single;

            IBaseService<T> svr = new BaseService<T>(LibGlobal.HbmSessionFactory.GetCurrentSession());

            long total=0;

            Action queryAction = () =>
                {
                    IQuery qry = svr.GetListByHQLWithPaging2(pq.HQL, pq.TotalHQL, pq.FirstResult, PAGE_SIZE, out total);

                    gvList.ItemsSource = qry.List();

                    //IQuery q = null;

                    //gvList.ItemsSource = qry.Enumerable();

                    dpList.ItemCount = (int)total;
                };

            dpList.PageIndexChanged += (object sender, PageIndexChangedEventArgs e) =>
                {
                    if (pq != null)
                    {
                        pq.FirstResult = e.NewPageIndex * PAGE_SIZE;
                        queryAction();
                    }
                };

            action = () =>
            {
                if (gvList.SelectedItem!=null)
                {
                    result = gvList.SelectedItem as T;
                }
                Close();
            };

            queryAction();

            ShowWindow(true);
            return result;
        }

        public IList<T> MultiSelecteByList<T>(IList<T> ls)
        {
            IList<T> result = new List<T>();


            gvList.SelectionMode=SelectionMode.Multiple;
            gvList.ItemsSource = ls;

            action = () =>
            {
                if (gvList.SelectedItems != null && gvList.SelectedItems.Count > 0)
                {
                    result=gvList.SelectedItems.Cast<T>().ToList<T>();
                }
                Close();
            };
            ShowWindow(true);
            return result;
        }


        private void dpList_PageIndexChanged(object sender, Telerik.Windows.Controls.PageIndexChangedEventArgs e)
        {

        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            if(action!=null)
                action();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void gvList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (action != null)
                action();
        }
    }
}
